// 这个js文件是可以设置通过ctrl+s来保存文本
iframe=window.parent
//这里来绑定ctrl+s可以用来保存
document.addEventListener('keydown', function(e) 
{
    console.log("ctrl+s.js中")
    if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey))
    {
        e.preventDefault();  
        saveData();
    }
});




function saveData()
{ 

    var title=document.getElementsByClassName("view")[1].firstElementChild.innerText.replace(/\s*/ig,"")
    var cls=iframe.document.getElementById("cls").value.replace(/\s*/ig,"")
    var dataFlag=cls+"#-#"+title
    var content=document.getElementsByClassName("view")[1].innerHTML
    var oDataFlage=iframe.document.getElementById("oDataFlag").value.replace(/\s*/ig,"")
    var mediaList=document.getElementsByClassName("view")[1].getElementsByClassName("#-media")
    var media=[]

    if(title.length==0)
    {
        alert("第一行为空")
        return  
    }
    if(cls.length==0)
    {
        alert("请输入分类")
        return  
    }
    for(var i=0;i<mediaList.length;i++)
    {  
        var Ur=mediaList[i].getAttribute("src")
        if(Ur==null)
        {
             Ur=mediaList[i].getAttribute("href")
        }
        media.push(Ur)
    }
    oMedia=media

    var data={
            "dataFlag":dataFlag,
            "content":content,
            "oDataFlag":oDataFlage,
            "media":media,
            "subEd":iframe.document.getElementById("subEd").value,
            "ls=":cls,
            "fc":"saveData"
    }
    
   
    ajax(data,"dealCenter.php","json",function(e)
    {
        console.log(e)
        iframe.document.getElementById("subm").setAttribute("onclick","")
        iframe.document.getElementById("subm").style.color="#eee"
        if(e=="ok")
        {
            iframe.document.getElementById("status").innerText="保存成功"
            iframe.document.getElementById("topStatue").innerText="保存成功"
            iframe.document.getElementById("subEd").value="yes"
            iframe.document.getElementById("subm").setAttribute("onclick","saveData()")
            iframe.document.getElementById("subm").style.color="black"
            iframe.document.getElementById("oDataFlag").value=dataFlag
            iframe.document.getElementById("oMedia").value=media
            iframe.document.getElementById("contents").value=content
            setTimeout(function(){
            iframe.document.getElementById("status").innerText=""
            iframe.document.getElementById("topStatue").innerText=""
            },3000)
        }
        else if(e=="changeNameOrClass")
        {
            alert("名称和类型都重复")
            document.getElementById("subEd").value="no"
            document.getElementById("subm").setAttribute("onclick","saveData()")
            document.getElementById("subm").style.color="black"
        }
        else
        {
            console.log(e)
            iframe.document.getElementById("status").innerText="保存失败"
            iframe.document.getElementById("topStatue").innerText=""
            setTimeout(function(){
            iframe.document.getElementById("subm").setAttribute("onclick","saveData()")
            iframe.document.getElementById("subm").style.color="black"

            },3000)
        }
       
    })

   
}



function ajax(data,ulr,type,func)
{
    var httpRequest=new XMLHttpRequest()
    httpRequest.open("POST",ulr,true)
    
    if(type=="text")
    {
        
    }
    if(type=="json")
    {
        var head="application/json"
        httpRequest.setRequestHeader("Content-Type", head); 
    }
    
    httpRequest.onreadystatechange = function () 
    {
        if (httpRequest.readyState == 4 && httpRequest.status == 200)
        {
            flag=httpRequest.responseText.replace(/\s*/ig,"")
            return func(flag)
        }
    }
    
    if(type=="json")
    {
        httpRequest.send(JSON.stringify(data))
    }
    else
    {
          httpRequest.send(data)
    }
}